/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgjeudestrategie;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JComponent;

/**
 *
 * @author Admin
 */
public class Drapeau extends JComponent{
    static ArrayList<Drapeau> drapeauList=new ArrayList();
    int x;
    String s="";
    boolean relocate;
    boolean red=false,blue=false,white=false,green=false,yellow=false,black=false;
    Color c1=null,c2=null,c3=null,c4=null;
    public Drapeau(){
        boolean eternity=true;
        
        if(drapeauList.size()>0){
            while(eternity){
            if(isNotTaken()){
                
        drapeauList.add(this);
       setSize(400,400);
        eternity=false;
            }
            else{
               create();
            }
        }}
        else{
            create(); 
        }
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                relocate=true;
                

            }
        });
        
    }
    public boolean isRelocate(){
        return relocate;
    }
    public void setRelocate(boolean b){
        relocate=b;
    }
    public void create(){
        Random rnd=new Random();
         x=rnd.nextInt(12);
               newValor(1);
            newValor(2);
        
            newValor(3);
            newValor(4);
        
    }
    public int getX(){
        return x;
    }
    public Color getC1(){
        return c1;
    }
    public Color getC2(){
        return c2;
    }
    public Color getC3(){
        return c3;
    }
    public Color getC4(){
        return c4;
    }
        private boolean isNotTaken(){
        for(Drapeau d:drapeauList){
            if(x==d.getX()){
                if(c1==d.getC1()){
                  if(c2==d.getC2()){
                    if(c3==d.getC3()){
                        if(c4==d.getC4()){
                    return false;}
                }
                }  
                }
            }
        }
        
        return true;
    }
        public void setAName(String s){
            this.s=s;
        }
    private void newValor(int i){
        //rouge,bleu,blanc,vert,jaune,noir
        Random rnd=new Random();
        int x=rnd.nextInt(6);
        Color c= Color.BLACK;
        if(x==0&&!red){
            c= Color.RED;
        red=true;}
        else if(x==1&&!blue){
            c= Color.blue;
            blue=true;}
        
        else if(x==2&&!white){
            c= Color.white;
            white=true;
        }
        else if(x==3&&!green){
            c= new Color(0, 150, 0);
            green=true;}
        
        else if(x==4&&!yellow){
            
            c= Color.yellow;
            yellow=true;}
        
        else if(x==5&&!black){
            
            c= Color.black;
            black=true;
        }
        
        if(i==1){
            c1=c;
        }
        else  if(i==2){
            c2=c;
        }
        else if(i==3){
            c3=c;
        }
        else if(i==4){
            c4=c;
        }
        
    }
    public void paintComponent(Graphics g){
        g.setColor(c1);
        g.fillRect(80, 0, (int) (getWidth()/4), (int) (getWidth()/4));
        
        g.setColor(c2);
        if(x==0){
            
        g.fillRect((int) (getWidth()/8)+80, 0, (int) (getWidth()/8)+1, (int) (getWidth()/4));
        }
        else if(x==1){
            
        g.fillRect(80, (int) (getWidth()/8), (int) (getWidth()/4), (int) (getWidth()/8)+1);
        }
       else  if(x==2){
            
        g.fillRect((int) (getWidth()/10)+80, 0, (int) (getWidth()/20), (int) (getWidth()/4));
        g.fillRect(80, (int) (getWidth()/10), (int) (getWidth()/4), (int) (getWidth()/20));
        }
       else  if(x==3){
            
        g.fillOval((int) (getWidth()/16)+80, (int) (getWidth()/16), (int) (getWidth()/8), (int) (getWidth()/8));
        }
        
        else if(x==4){
            
        g.setColor(c2);
        g.fillRect((int) (getWidth()/12)+80, 0, (int) (getWidth()/12), (int) (getWidth()/4));
        g.setColor(c3);
        g.fillRect((int) (getWidth()/12)+80, 0, (int) (getWidth()/12), (int) (getWidth()/4));
        }
        else if(x==5){
            
        g.setColor(c2);
        g.fillRect(80,(int) (getWidth()/12), (int) (getWidth()/4), (int) (getWidth()/12));
        g.setColor(c3);
        g.fillRect(80, (int) (getWidth()/12), (int) (getWidth()/4),(int) (getWidth()/12));
        }
        else if(x==6||x==7||x==8||x==9||x==10||x==11){
            if(x==6||x==9){
                
        g.setColor(c2);
        g.fillRect(80, (int) (getWidth()/8), (int) (getWidth()/4), (int) (getWidth()/8)+1);
            }
            if(x==7||x==10){
        g.setColor(c2);
        g.fillRect(80,(int) (getWidth()/12), (int) (getWidth()/4), (int) (getWidth()/12));
        g.setColor(c3);
        g.fillRect(80, (int) (getWidth()/12), (int) (getWidth()/4),(int) (getWidth()/12));
                
            }
            if(x==6||x==7||x==8){
        g.setColor(c4);
            Point point1 = new Point(80,0);
            Point point2 = new Point((int) ((3*getWidth())/20)+80,(int) (getWidth()/8));
            Point point3 = new Point(80,(int) (getWidth()/4));
            int[] x = { point1.x, point2.x, point3.x };
            int[] y = { point1.y, point2.y, point3.y };
            Polygon triangle = new Polygon(x, y, x.length);
            g.fillPolygon(triangle);
        }
        
        if(x==9||x==10||x==11){
            
        g.setColor(c4);
        g.fillRect(80, 0, (int) (getWidth()/12),(int) (getWidth()/4));
        
        }
        
        }
     
        g.setColor(Color.black);
        g.drawRect(80, 0, (int) (getWidth()/4), (int) (getWidth()/4));   
        g.drawString(s, 0, 30);
        
    }
}
